
package com.joysuch.wwyt.mh.controller;


import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.mh.bean.MhAlarmMessageListBean;
import com.joysuch.wwyt.mh.bean.MhAlarmSettingBean;
import com.joysuch.wwyt.mh.entity.MhAlarmMessage;
import com.joysuch.wwyt.mh.service.MhAlarmMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mh/alarm-message")
@Api(tags = "/mh/alarm-message 报警")
public class MhAlarmMessageController {


    @Autowired
    private MhAlarmMessageService messageService;

    @Value(value = "${loginSuccessUrl}")
    private String loginSuccessUrl;


    @GetMapping("/list")
    public ResultBean list(Integer page, Integer size, String detail, Long alarmTypeId, Integer level, Integer state) {
        Page<MhAlarmMessageListBean> byPage = messageService.getByPage(QPageRequest.of(page, size), detail, alarmTypeId, level, state);
        return ResultBean.pageData(byPage.getContent(), byPage.getTotalElements());
    }


    @GetMapping("/getById")
    public ResultBean getById(Long id) {
        MhAlarmMessageListBean byId = messageService.getById(id);
        return ResultBean.wrapSuccessfulResult(byId);
    }

    @PostMapping("/save")
    public ResultBean save(Long id) {
        messageService.save(id);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/te")
    public void tes() {
        messageService.te();
    }


    @PostMapping("saveAlarmSetting")
    @ApiOperation(value = "保存设备报警配置")
    public ResultBean saveAlarmSetting(@RequestBody List<MhAlarmSettingBean> beans) {
        messageService.saveAlarmSetting(beans);
        return ResultBean.success("");
    }

    @GetMapping("getAlarmSettings")
    @RequiresPermissions("mh:message:list")
    @ApiOperation(value = "查看报警配置列表", response = List.class)
    public ResultBean getAlarmSettings() {
        List<MhAlarmSettingBean> beans = messageService.getAlarmSettings();
        return ResultBean.success(beans);
    }

    @PostMapping("/test")
    @ApiOperation(value = "新报警测试")
    public ResultBean test(Long id) {
        messageService.test(id);
        return ResultBean.success("");
    }
}
